package org.example.controller;

import com.google.common.collect.ImmutableMap;
import org.example.entity.SpecKey;
import org.example.model.PageBean;
import org.example.service.SpecKeyService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/spec-key")
public class SpecKeyController {
    @Resource
    private SpecKeyService specKeyService;

    @GetMapping
    public List<SpecKey> getAll(){
        return specKeyService.getAll();
    }

    @GetMapping("/pg")
    public PageBean<SpecKey> specKeyList(@RequestParam(defaultValue = "1") Integer page,
                                           @RequestParam(defaultValue = "5") Integer size){
        return specKeyService.specKeyList(page,size);
    }

    @PostMapping
    public Map<String ,Object> addSpecValue(@RequestBody SpecKey specKey){
        specKeyService.addSpec(specKey);
        return ImmutableMap.of("code", 1, "msg", "success");
    }

    @GetMapping("/{id}")
    public SpecKey getSpecValueById(@PathVariable("id") Integer id){
        return specKeyService.getSpecKey(id);
    }

    @PutMapping
    public Map<String, Object> edit(@RequestBody SpecKey specKey) {
        specKeyService.editSpec(specKey);
        return ImmutableMap.of("code", 1, "msg", "success");
    }

    @DeleteMapping("/{id}")
    public Map<String ,Object> deleteSpecValue(@PathVariable("id") Integer id){
        specKeyService.deleteSpecKey(id);
        return ImmutableMap.of("code", 1, "msg", "success");
    }
}
